"use client"
import React, { useEffect, useRef, useState } from "react";
import * as THREE from "three";
import Image from 'next/image';
// import "antd/dist/antd.css";

// 用户图标
const UserIcon = () => (
  <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z" />
  </svg>
);

// 锁图标
const LockIcon = () => (
  <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
  </svg>
);

// 眼睛图标（用于密码显示/隐藏）
const EyeIcon = () => (
  <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />
  </svg>
);

// 眼睛关闭图标（用于密码显示/隐藏）
const EyeOffIcon = () => (
  <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21" />
  </svg>
);

export default function AiLoginPage() {
  const threeRef:React.RefObject<any> = useRef(null);
  
  // 密码显示/隐藏状态
  const [showPassword, setShowPassword] = useState(false);
  // 表单数据
  const [formData, setFormData] = useState({
    email: '',
    password: ''
  });

  useEffect(() => {
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      1000
    );
    const renderer = new THREE.WebGLRenderer({ alpha: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    threeRef.current&&threeRef.current.appendChild(renderer.domElement);

    // 创建多个节点（神经元）
    const neuronMaterial = new THREE.MeshBasicMaterial({ color: 0x00ffe5 });
    const connectionMaterial = new THREE.LineBasicMaterial({ color: 0x00ffe5, transparent: true, opacity: 0.3 });
    const neurons = [];


    for (let i = 0; i < 30; i++) {
      const geometry = new THREE.SphereGeometry(0.08, 16, 16);
      const neuron = new THREE.Mesh(geometry, neuronMaterial);
      neuron.position.set(
        (Math.random() - 0.5) * 10,
        (Math.random() - 0.5) * 6,
        (Math.random() - 0.5) * 8
      );
      scene.add(neuron);
      neurons.push(neuron);
    }

    // 创建连接线（模拟神经连接）
    for (let i = 0; i < neurons.length; i++) {
      for (let j = i + 1; j < neurons.length; j++) {
        if (Math.random() < 0.1) { // 控制连接密度
          const geometry = new THREE.BufferGeometry().setFromPoints([
            neurons[i].position,
            neurons[j].position
          ]);
          const line = new THREE.Line(geometry, connectionMaterial);
          scene.add(line);
        }
      }
    }

    camera.position.z = 10;

    const animate = function () {
      requestAnimationFrame(animate);
      scene.rotation.y += 0.0015;
      scene.rotation.x += 0.0005;
      renderer.render(scene, camera);
    };

    animate();

    const handleResize = () => {
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
      renderer.setSize(window.innerWidth, window.innerHeight);
    };

    window.addEventListener("resize", handleResize);

    return () => {
      window.removeEventListener("resize", handleResize);
      if (threeRef.current) {
        threeRef.current.removeChild(renderer.domElement);
      }
    };
  }, []);

  return (
    <div className="relative h-screen w-screen overflow-hidden bg-gradient-to-br from-[#0a0a1f] via-[#1f1f3a] to-[#0a0a1f] flex items-center justify-center">
      {/* Three.js AI 神经线图背景 */}
      <div ref={threeRef} className="absolute inset-0 z-0" />

      {/* 登录卡片 */}
      <div className="z-10 backdrop-blur-md bg-white/5 border border-white/10 rounded-2xl p-10 w-[360px] shadow-2xl">
        <div className="text-center mb-8">
        <Image src="/logo.svg" alt="AI Logo" className="w-12 h-12 mx-auto mb-2" />
        <h1 className="text-2xl font-bold text-white mb-2">AI Matrix</h1>
        <p className="text-gray-300">智能驱动未来</p>
      </div>

        <div className="space-y-4">
          {/* 邮箱输入框 */}
          <div className="relative">
            <div className="absolute left-3 top-1/2 transform -translate-y-1/2">
              <UserIcon />
            </div>
            <input
              type="email"
              value={formData.email}
              onChange={(e) => setFormData({...formData, email: e.target.value})}
              placeholder="Email"
              className="w-full pl-10 pr-4 py-3 rounded-lg bg-white/10 border border-white/20 text-white placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-cyan-500"
            />
          </div>
          
          {/* 密码输入框 */}
          <div className="relative">
            <div className="absolute left-3 top-1/2 transform -translate-y-1/2">
              <LockIcon />
            </div>
            <input
              type={showPassword ? "text" : "password"}
              value={formData.password}
              onChange={(e) => setFormData({...formData, password: e.target.value})}
              placeholder="Password"
              className="w-full pl-10 pr-12 py-3 rounded-lg bg-white/10 border border-white/20 text-white placeholder:text-gray-400 focus:outline-none focus:ring-2 focus:ring-cyan-500"
            />
            <button
              type="button"
              onClick={() => setShowPassword(!showPassword)}
              className="absolute right-3 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-white transition-colors"
              aria-label={showPassword ? "隐藏密码" : "显示密码"}
            >
              {showPassword ? <EyeOffIcon /> : <EyeIcon />}
            </button>
          </div>
          
          {/* 登录按钮 */}
          <button
            type="button"
            className="w-full py-3 rounded-lg bg-gradient-to-r from-cyan-400 to-teal-400 text-white font-medium hover:from-cyan-500 hover:to-teal-500 transition-all duration-300"
          >
            登录
          </button>
          
          <div className="flex justify-between text-sm text-gray-400">
            <a href="#" className="hover:text-cyan-400 transition">忘记密码？</a>
            <a href="#" className="hover:text-cyan-400 transition">注册账号</a>
          </div>
        </div>
      </div>
    </div>
  );
}
